﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.ParkManage;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static LJX.SmartPark.Service.AssetManage.EquipmentLedgerServeice;

namespace LJX.SmartPark.Service.ParkManage
{
    public class ContractManageServeice: IContractManageServeice
    {
        private readonly IRepository<ContractManage> _contractManageServeice;

        public ContractManageServeice(IRepository<ContractManage> contractManageServeice)
        {
            _contractManageServeice = contractManageServeice;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> DeleteContractManageAsync(ContractManage input)
        {
            var result = await _contractManageServeice.DeleteByIdAsync(input.MapTo<ContractManage>());
            return Response<bool>.Ok(result > 0);
        }

        /// <summary>
        /// 获取总条数
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            return _contractManageServeice.GetCount();
        }

        /// <summary>
        /// 显示分页
        /// </summary>
        /// <param name="fyContractManage"></param>
        /// <returns></returns>
        public async Task<Response<List<ContractManage>>> GetContractManageAsync(FyContractManage fyContractManage)
        {
            var contractManage = await _contractManageServeice.GetAllListAsync();
            var result = contractManage.MapToList<ContractManage>();

            if (!string.IsNullOrEmpty(fyContractManage.titleContract))
            {
                result = result.Where(a => a.TitleContract.Contains(fyContractManage.titleContract)).ToList();
            }

            if (!string.IsNullOrEmpty(fyContractManage.oppositeUnit))
            {
                result = result.Where(a => a.OppositeUnit.Contains(fyContractManage.oppositeUnit)).ToList();
            }

            if (!string.IsNullOrEmpty(fyContractManage.oppositeParty))
            {
                result = result.Where(a => a.OppositeParty.Contains(fyContractManage.oppositeParty)).ToList();
            }

            if (!string.IsNullOrEmpty(fyContractManage.agent))
            {
                result = result.Where(a => a.Agent.Contains(fyContractManage.agent)).ToList();
            }

            var totalCount = result.Count();

            result = result.Skip(((fyContractManage.pageIndex - 1) * fyContractManage.pageSize)).Take(fyContractManage.pageSize).ToList();

            return Response<List<ContractManage>>.Ok(result, totalCount);

        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Response<ContractManage> GetContractManageById(ContractManage input)
        {
            var contractManage = _contractManageServeice.GetListQuery().FirstOrDefault(m => m.Id == input.Id);
            var result = contractManage.MapTo<ContractManage>();
            return Response<ContractManage>.Ok(result);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PostAddContractManageAsync(ContractManage input)
        {
            var result = _contractManageServeice.InsertAsyncById(input.MapTo<ContractManage>());

            return Response<bool>.Ok(result.Result > 0);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PutUpdContractManageAsync(ContractManage input)
        {
            var result = _contractManageServeice.UpdateAsync(input.MapTo<ContractManage>());

            return Response<bool>.Ok(result.Result > 0);
        }

        /// <summary>
        /// 分页
        /// </summary>
        public class FyContractManage
        {
            public int pageIndex { get; set; }
            public int pageSize { get; set; }
            public string? titleContract { get; set; }
            public string? oppositeUnit { get; set; }
            public string? oppositeParty { get; set; }
            public string? agent { get; set; }
        }
    }
}
